﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sanford.Multimedia.Midi;

namespace MidiMix
{
    public partial class ChannelStripCollection : UserControl
    {
                private Color[] colors = {Color.Red, Color.Orange, Color.Yellow, Color.Green, 
                                 Color.DarkBlue, Color.LightBlue, Color.DarkSlateGray,
                             Color.Red, Color.Orange, Color.Yellow, Color.Green, 
                                 Color.DarkBlue, Color.LightBlue, Color.DarkSlateGray};

        private int numChannels = 0;
        public int NumChannels {
            get {return numChannels;}
        }
        public int SoloCount { 
            get 
            {
                int count=0;
                
                foreach (ChannelStripSettings css in dictTrackCSS.Values) {
                    if (css.Solo) count++;
                }
                return count;
            } 
        }
        public Dictionary<int, ChannelStripSettings>.Enumerator ChannelStripSettingsEnumerator
        {
            get { return dictTrackCSS.GetEnumerator(); }
        }
        public void UpdateControls()
        {
            foreach (Control c in Controls)
            {
                ChannelStrip cs = (ChannelStrip)c;
                cs.UpdateControls();
            }
        }
        private Dictionary<int, ChannelStripSettings> dictTrackCSS = new Dictionary<int,ChannelStripSettings> ();
       
        public ChannelStripCollection()
        {
            InitializeComponent();
        }


        public ChannelStripSettings GetChannelStripSettingsForTrack(int trackIndex)
        {
            if (dictTrackCSS.ContainsKey(trackIndex))
                return dictTrackCSS[trackIndex];
            else
                return null;
        }

        public void initControls(Sequence sequence)
        {
            dictTrackCSS.Clear();
            numChannels=0;
            this.Controls.Clear();

            for (int i = 0; i < sequence.Count; i++)
            {
                Track t = sequence[i];
                bool hasNotes = false;
                for (int j=0; j < t.Count; j++)
                {
                    MidiEvent me = t.GetMidiEvent(j);
                    if (me.MidiMessage.MessageType == MessageType.Channel)
                    {
                        ChannelMessage cm = (ChannelMessage)me.MidiMessage;
                        if (cm.Command == ChannelCommand.NoteOn)
                        {
                            hasNotes = true;
                            break;
                        }
                    }
                }
                if (hasNotes)
                {
                    ChannelStrip cs = addChannelStrip(t);
                    dictTrackCSS.Add(i, cs.ChannelStripSettings);
                }
                layoutControls();
            }
        }
        private ChannelStrip addChannelStrip(Track t)
        {
            ChannelStrip cs = new ChannelStrip();
            cs.TrackNum = Convert.ToString(numChannels + 1);
            cs.Anchor = AnchorStyles.Bottom | AnchorStyles.Top;
            cs.TrackName = "Track " + (numChannels + 1);
            cs.TrackColor = (numChannels < colors.Length) ? colors[numChannels] : Color.SkyBlue;
            cs.TrackName = t.TrackName;

            this.Controls.Add(cs);
            numChannels++;
            layoutControls();
            return cs;
        }
        private void layoutControls()
        {
            int i = 0;
            foreach (ChannelStrip cs in this.Controls)
            {
                cs.Top = 0;
                cs.Left = cs.Width * i++;
                cs.Height = this.Height;
            }
        }


        private void ChannelStripCollection_Load(object sender, EventArgs e)
        {
        }

        private void ChannelStripCollection_Resize(object sender, EventArgs e)
        {
            layoutControls();
        }
    }
}
